using System;
using System.Collections.Generic;
using System.Text;

using SBPweb.Modules.Modularity;

namespace SBPweb.Contracts.Implementation
{
	/// <summary>
	/// Class of LayoutGroup
	/// </summary>
	public class LayoutGroup : IPersistable
	{
		#region Private members

		private IPersistable pParentPersistable;
		private IStatePersistenceService pPersistenceService;
		private string pLayoutGroupName;
		private string pSelectedLayout;
		private Dictionary<string, Layout> pLayouts;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of LayoutGroup
		/// </summary>
		/// <param name="parent"></param>
		/// <param name="persistenceService"></param>
		/// <param name="layoutGroupName"></param>
		public LayoutGroup(IPersistable parent, IStatePersistenceService persistenceService, string layoutGroupName)
		{
			pParentPersistable = parent;
			pPersistenceService = persistenceService;
			pLayoutGroupName = layoutGroupName;
			pSelectedLayout = string.Empty;
			pLayouts = null;
		}

		#endregion

		#region Public properties

		/// <summary>
		/// Return parent as persitable
		/// </summary>
		public IPersistable ParentPersistable
		{
			get 
			{
				return pParentPersistable;
			}
		}

		/// <summary>
		/// Persistency key
		/// </summary>
		public string PersistencyKey
		{
			get 
			{
				return string.Format("{0} - {1}", this.GetType().Name, pLayoutGroupName);
			}
		}

		/// <summary>
		/// Layout group name
		/// </summary>
		public string LayoutGroupName
		{
			get
			{
				return pLayoutGroupName;
			}
			internal set
			{
				pLayoutGroupName = value;
			}
		}

		/// <summary>
		/// Selected layout
		/// </summary>
		public string SelectedLayout
		{
			get
			{
				return pSelectedLayout;
			}
			internal set
			{
				pSelectedLayout = value;
			}
		}

		/// <summary>
		/// Dictionary of layouts
		/// </summary>
		public Dictionary<string, Layout> Layouts
		{
			get
			{
				return pLayouts;
			}
		}

		#endregion

		#region Public methods

		/// <summary>
		/// Load persistency
		/// </summary>
		public void LoadPersistency()
		{
			if (pPersistenceService != null)
			{
				PersistencyDictionary state = pPersistenceService.Load(this);
				pSelectedLayout = state.GetString("SelectedLayout", string.Empty);

				PersistableList layoutNames = new PersistableList("LayoutNames", this, pPersistenceService);
				layoutNames.LoadPersistency();
				pLayouts = new Dictionary<string, Layout>();
				foreach (string name in layoutNames)
				{
					pLayouts.Add(name, new Layout(this, pPersistenceService, name, string.Empty));
					pLayouts[name].LoadPersistency();
				}
			}
		}

		/// <summary>
		/// Save persistency
		/// </summary>
		public void SavePersistency()
		{
			if (pPersistenceService != null)
			{
				PersistencyDictionary state = pPersistenceService.Load(this);
				state.SetString("SelectedLayout", pSelectedLayout);
				pPersistenceService.Save(this, state);

				PersistableList layoutNames = new PersistableList("LayoutNames", this, pPersistenceService);
				foreach (string name in pLayouts.Keys)
				{
					layoutNames.Add(name);
					pLayouts[name].SavePersistency();
				}
				layoutNames.SavePersistency();
			}
		}			

		#endregion
	}
}
